package com.panfeng.xcloud.common.core.utils;

import com.panfeng.xcloud.common.core.constants.BlConstants;
import com.panfeng.xcloud.common.core.constants.RedisConstants;
import com.panfeng.xcloud.common.core.enums.ExchangeTypeEnum;
import com.panfeng.xcloud.common.core.enums.ResponseStatusEnum;
import com.panfeng.xcloud.common.core.exceptions.BaseBizException;

public class KeyContactUtil {

    /**
     * 拼成机器人的redis key
     *
     * @return
     */
    public static String getUserBotKey(Integer exchangeType, String direction, Integer leverRate, String destinationCoin, String sourceCoin, String userId) {
        StringBuffer key = new StringBuffer();
        if (exchangeType == ExchangeTypeEnum.OKEX.getCode()) {
            StringBuffer result = key.append(BlConstants.ROBOT_OKEX).append(direction).append(BlConstants.ROBOT_SPLIT).append(leverRate).append(BlConstants.ROBOT_SPLIT).
                    append(getContractCode(exchangeType, destinationCoin, sourceCoin)).append(BlConstants.ROBOT_SPLIT).append(userId);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.HUOBI.getCode()) {
            StringBuffer result = key.append(BlConstants.ROBOT_HUOBI).append(direction).append(BlConstants.ROBOT_SPLIT).append(leverRate).append(BlConstants.ROBOT_SPLIT).
                    append(getContractCode(exchangeType, destinationCoin, sourceCoin)).append(BlConstants.ROBOT_SPLIT).append(userId);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.BINANCE.getCode()) {
            StringBuffer result = key.append(BlConstants.ROBOT_BINANCE).append(direction).append(BlConstants.ROBOT_SPLIT).append(leverRate).append(BlConstants.ROBOT_SPLIT).
                    append(getContractCode(exchangeType, destinationCoin, sourceCoin)).append(BlConstants.ROBOT_SPLIT).append(userId);
            return result.toString();
        } else {
            throw new BaseBizException(ResponseStatusEnum.EXCHANGE_TYPE_NOT_EXIST);
        }
    }

    /**
     * 对冲仓的redis key
     *
     * @return
     */
    public static String getOffSetKey(Integer exchangeType, String direction, Integer leverRate, String destinationCoin, String sourceCoin, String userId) {
        StringBuffer key = new StringBuffer();
        if (exchangeType == ExchangeTypeEnum.OKEX.getCode()) {
            StringBuffer result = key.append(RedisConstants.OFF_SET_DETAIL_HUOBI).append(direction).append(BlConstants.ROBOT_SPLIT).append(leverRate).append(BlConstants.ROBOT_SPLIT).
                    append(getContractCode(exchangeType, destinationCoin, sourceCoin)).append(BlConstants.ROBOT_SPLIT).append(userId);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.HUOBI.getCode()) {
            StringBuffer result = key.append(RedisConstants.OFF_SET_DETAIL_HUOBI).append(direction).append(BlConstants.ROBOT_SPLIT).append(leverRate).append(BlConstants.ROBOT_SPLIT).
                    append(getContractCode(exchangeType, destinationCoin, sourceCoin)).append(BlConstants.ROBOT_SPLIT).append(userId);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.BINANCE.getCode()) {
            StringBuffer result = key.append(RedisConstants.OFF_SET_DETAIL_BINANCE).append(direction).append(BlConstants.ROBOT_SPLIT).append(leverRate).append(BlConstants.ROBOT_SPLIT).
                    append(getContractCode(exchangeType, destinationCoin, sourceCoin)).append(BlConstants.ROBOT_SPLIT).append(userId);
            return result.toString();
        } else {
            throw new BaseBizException(ResponseStatusEnum.EXCHANGE_TYPE_NOT_EXIST);
        }
    }

    /**
     * 拼成合约的名称
     *
     * @return
     */
    public static String getContractCode(Integer exchangeType, String destinationCoin, String sourceCoin) {
        StringBuffer contractCode = new StringBuffer();
        if (exchangeType == ExchangeTypeEnum.OKEX.getCode()) {
            StringBuffer result = contractCode.append(destinationCoin).append(BlConstants.CONTRACTCODE_SPLIT).append(sourceCoin)
                    .append(BlConstants.CONTRACTCODE_SPLIT).append("SWAP");
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.HUOBI.getCode()) {
            StringBuffer result = contractCode.append(destinationCoin).append(BlConstants.CONTRACTCODE_SPLIT).append(sourceCoin);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.BINANCE.getCode()) {
            StringBuffer result = contractCode.append(destinationCoin).append(sourceCoin);
            return result.toString();
        } else {
            throw new BaseBizException(ResponseStatusEnum.EXCHANGE_TYPE_NOT_EXIST);
        }
    }

    /**
     * 获取量化展示用行情redis key
     *
     * @return
     */
    public static String getAppearMarketKey(Integer exchangeType, String destinationCoin, String sourceCoin) {
        StringBuffer key = new StringBuffer();
        if (exchangeType == ExchangeTypeEnum.OKEX.getCode()) {
            StringBuffer result = key.append(RedisConstants.HUO_BI_APPEAR_MARKET).append(BlConstants.ROBOT_SPLIT).append(destinationCoin).append(sourceCoin);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.HUOBI.getCode()) {
            StringBuffer result = key.append(RedisConstants.HUO_BI_APPEAR_MARKET).append(BlConstants.ROBOT_SPLIT).append(destinationCoin).append(sourceCoin);
            return result.toString();
        } else if (exchangeType == ExchangeTypeEnum.BINANCE.getCode()) {
            StringBuffer result = key.append(RedisConstants.BINANCE_APPEAR_MARKET).append(BlConstants.ROBOT_SPLIT).append(destinationCoin).append(sourceCoin);
            return result.toString();
        } else {
            throw new BaseBizException(ResponseStatusEnum.EXCHANGE_TYPE_NOT_EXIST);
        }
    }

    public static String getCollectUrlMapKey(String destinationCoin, String direction) {
        StringBuffer key = new StringBuffer();
        StringBuffer result = key.append(destinationCoin).append(BlConstants.ROBOT_SPLIT).append(direction);
        return result.toString();
    }

}
